package com.fortune.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.fortune.base.BaseController;
import com.fortune.dto.excution.DriverOrderExcution;
import com.fortune.dto.in.driverOrder.DriverOrderDeliveryDto;
import com.fortune.dto.in.driverOrder.OrderDetailsDto;
import com.fortune.entity.DriverOrderInfoDetailGy;
import com.fortune.enums.ClientEnum;
import com.fortune.service.DriverOrderService;
import com.fortune.vo.driverOrder.DriverOrderInfoDetail;
import com.fortune.vo.driverOrder.DriverPaymentInfor;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fortune.base.BaseAPIExcution;
import com.fortune.dto.in.driverOrder.DriverOrderPayedDto;
import com.fortune.vo.driverOrder.DriverOrderDetail;
import com.fortune.vo.driverOrder.DriverOrderInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import sun.util.calendar.BaseCalendar;

@Api(value = "wechat/driverOrder", description = "送货清单", tags = {"微信小程序（司机）：送货清单"})
@RestController
@RequestMapping("wechat/driverOrder")
public class DriverOrderController extends BaseController{
	@Autowired
	private DriverOrderService driverOrderService;

	@ApiOperation(value = "送货清单", nickname = "driverOrderList", notes = "", tags = {"@郭阳"})
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<DriverOrderInfo> list() {
		String mid = getMId();
		String did = getDId();
		String deliveryUser = getUserId();
		List<DriverOrderInfoDetailGy> findListDriverOrderInfoDetailGy = driverOrderService.findListDriverOrderInfoDetailGy(mid, did, deliveryUser);
		List<DriverOrderInfoDetail> toBeDelivered = new ArrayList<>();//待配送
		List<DriverOrderInfoDetail> completed = new ArrayList<>();//已完成
		for(int i = 0; i < findListDriverOrderInfoDetailGy.size(); i++){
			DriverOrderInfoDetailGy driverOrderInfoDetailGy = findListDriverOrderInfoDetailGy.get(i);
			String isDelivery = driverOrderInfoDetailGy.getIsDelivery().toString();
			DriverOrderInfoDetail driverOrderInfoDetail = new DriverOrderInfoDetail();
			if(isDelivery.equals("NO")){//待配送
				String no = driverOrderInfoDetailGy.getNo();
				BigDecimal pTotalPrice = driverOrderInfoDetailGy.getpTotalPrice();
				String name = driverOrderInfoDetailGy.getName();
				String abbreviation = driverOrderInfoDetailGy.getAbbreviation();
				String number = driverOrderInfoDetailGy.getNumber();
				String principalName = driverOrderInfoDetailGy.getPrincipalName();
				String principalPhone = driverOrderInfoDetailGy.getPrincipalPhone();
				String address = driverOrderInfoDetailGy.getAddress();
				float longitude = driverOrderInfoDetailGy.getLongitude();
				float latitude = driverOrderInfoDetailGy.getLatitude();
				String orderId = driverOrderInfoDetailGy.getOrderId();
				String id = driverOrderInfoDetailGy.getId();
				driverOrderInfoDetail.setNo(no);
				driverOrderInfoDetail.setpTotalPrice(pTotalPrice);
				driverOrderInfoDetail.setName(name);
				driverOrderInfoDetail.setAbbreviation(abbreviation);
				driverOrderInfoDetail.setNumber(number);
				driverOrderInfoDetail.setPrincipalName(principalName);
				driverOrderInfoDetail.setPrincipalPhone(principalPhone);
				driverOrderInfoDetail.setAddress(address);
				driverOrderInfoDetail.setLongitude(longitude);
				driverOrderInfoDetail.setLatitude(latitude);
				driverOrderInfoDetail.setOrderId(orderId);
				driverOrderInfoDetail.setId(id);
				toBeDelivered.add(driverOrderInfoDetail);
			}
			if(isDelivery.equals("YES")){//已完成
				String no = driverOrderInfoDetailGy.getNo();
				BigDecimal pTotalPrice = driverOrderInfoDetailGy.getpTotalPrice();
				String name = driverOrderInfoDetailGy.getName();
				String abbreviation = driverOrderInfoDetailGy.getAbbreviation();
				String number = driverOrderInfoDetailGy.getNumber();
				String principalName = driverOrderInfoDetailGy.getPrincipalName();
				String principalPhone = driverOrderInfoDetailGy.getPrincipalPhone();
				String address = driverOrderInfoDetailGy.getAddress();
				float longitude = driverOrderInfoDetailGy.getLongitude();
				float latitude = driverOrderInfoDetailGy.getLatitude();
				String orderId = driverOrderInfoDetailGy.getOrderId();
				String id = driverOrderInfoDetailGy.getId();
				driverOrderInfoDetail.setNo(no);
				driverOrderInfoDetail.setpTotalPrice(pTotalPrice);
				driverOrderInfoDetail.setName(name);
				driverOrderInfoDetail.setAbbreviation(abbreviation);
				driverOrderInfoDetail.setNumber(number);
				driverOrderInfoDetail.setPrincipalName(principalName);
				driverOrderInfoDetail.setPrincipalPhone(principalPhone);
				driverOrderInfoDetail.setAddress(address);
				driverOrderInfoDetail.setLongitude(longitude);
				driverOrderInfoDetail.setLatitude(latitude);
				driverOrderInfoDetail.setOrderId(orderId);
				driverOrderInfoDetail.setId(id);
				completed.add(driverOrderInfoDetail);
			}
		}
		DriverOrderInfo driverOrderInfo = new DriverOrderInfo();
		driverOrderInfo.setToBeDelivered(toBeDelivered);
		driverOrderInfo.setCompleted(completed);
		return new DriverOrderExcution<DriverOrderInfo>(driverOrderInfo, ClientEnum.FIND_SUCCESS);
	}
	
	@ApiOperation(value = "订单详情", nickname = "driverOrderDetail", notes = "", tags = {"@郭阳"})
	@ApiImplicitParams({
		@ApiImplicitParam(name="orderId", value="订单id",paramType="query",dataType="string"),
		@ApiImplicitParam(name="clientId", value="客户id",paramType="query",dataType="string"),
		@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
    @GetMapping(value = "detail",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<DriverOrderDetail> detail(@Validated @NotBlank(message="订单id不能为空") String orderId,
			@Validated @NotBlank(message="客户id不能为空") String clientId) {
		String mid = getMId();
		String did = getDId();
		DriverOrderDetail findDriverOrderDetail = driverOrderService.findDriverOrderDetail(mid, did, clientId, orderId);
		BigDecimal pTotalPrice = findDriverOrderDetail.getdTotalPrice();
		BigDecimal dTotalPrice = findDriverOrderDetail.getdTotalPrice();
		BigDecimal totalMoney = pTotalPrice.add(dTotalPrice);
		findDriverOrderDetail.setTotalMoney(totalMoney);
		return new DriverOrderExcution<DriverOrderDetail>(findDriverOrderDetail,ClientEnum.FIND_SUCCESS);
	}
	
	@ApiOperation(value = "提交支付信息", nickname = "driverOrderPayed", notes = "", tags = {"@郭阳"})
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
    @PutMapping(value = "payed",
        produces = { MediaType.APPLICATION_JSON_VALUE },
        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> payed(@Validated @RequestBody DriverOrderPayedDto payDto) {
		String mid = getMId();
		String did = getDId();
		int editDriverPaymentStaus = driverOrderService.editDriverPaymentStaus(mid, did, payDto);
		if(editDriverPaymentStaus < 1){
			return new DriverOrderExcution<Void>( null,ClientEnum.EDIT_ERROR);
		}
		return new DriverOrderExcution<Void>(null,ClientEnum.EDIT_SUCCESS);
	}

	@ApiOperation(value = "支付信息", nickname = "driverOrderPayment", notes = "", tags = {"@郭阳"})
	@ApiImplicitParams({
			@ApiImplicitParam(name="clientId", value="客户id",paramType="query",dataType="string"),
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@GetMapping(value = "payment",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<DriverPaymentInfor> payment(@Validated @NotBlank(message="客户id不能为空") String clientId) {
		String mid = getMId();
		String did = getDId();
		DriverPaymentInfor findDriverPaymentInfor = driverOrderService.findDriverPaymentInfor(mid, did, clientId);
		if(findDriverPaymentInfor == null){
			return new DriverOrderExcution<DriverPaymentInfor>(null,ClientEnum.FIND_RESULT_AIR);
		}
		return new DriverOrderExcution<DriverPaymentInfor>(findDriverPaymentInfor,ClientEnum.FIND_SUCCESS);
	}

	@ApiOperation(value = "确认送达", nickname = "driverOrderDelivery", notes = "", tags = {"@郭阳"})
	@ApiImplicitParams({
			@ApiImplicitParam(name="Authorization", value="登录返回的token值",paramType="header",dataType="String",required = true)
	})
	@PutMapping(value = "delivery",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delivery(@Validated @RequestBody DriverOrderDeliveryDto driverOrderDeliveryDto) {
		String mid = getMId();
		String did = getDId();
		String clientId = driverOrderDeliveryDto.getClientId();
		String orderId = driverOrderDeliveryDto.getOrderId();
		List<OrderDetailsDto> orderDetailsDto = driverOrderDeliveryDto.getOrderDetailsDto();
		for(int i = 0; i < orderDetailsDto.size(); i++){
			OrderDetailsDto orderDetailsDto1 = orderDetailsDto.get(i);
			String no = orderDetailsDto1.getNo();
			float backBoxNum = orderDetailsDto1.getBackBoxNum();
			BigDecimal dprice = orderDetailsDto1.getDprice();
			int editOrderDelivery = driverOrderService.editOrderDelivery(mid, did, clientId, orderId, no, backBoxNum, dprice);
			if(editOrderDelivery < 1){
				return new DriverOrderExcution<Void>(null,ClientEnum.EDIT_ERROR);
			}
		}
		return new DriverOrderExcution<Void>(null,ClientEnum.EDIT_SUCCESS);
	}
}
